package com.pirestupppai.widgets.view;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.pirestupppai.R;
import com.pirestupppai.bean.FeedItem;
import com.pirestupppai.common.Constant;
import com.pirestupppai.network.NetworkRequest;
import com.pirestupppai.network.TupaiSubscriber;
import com.pirestupppai.ui.activity.ModuleActivity;
import com.pirestupppai.utils.ImageLoadUtil;

import java.util.ArrayList;
import java.util.List;



/**
 * 单个模块视图
 *
 * @author: xiaoluo
 * @date: 2017-04-10 18:10
 */
public class SingleModuleView extends LinearLayout {
    private Context mContext;

    public static final String TYPE_HOT = "hotreply";
    public static final String TYPE_ASK = "ask";
    public static final String TYPE_REPLY = "reply";

    private LinearLayout mParentlayout;
    private TextView mTitleTv;
    private ImageView mImageOne;
    private ImageView mImageTwo;
    private ImageView mImageThree;
    private ImageView mImageFour;

    private List<String> mImageList = new ArrayList<>();
    private String mType;

    public SingleModuleView(Context context, String type) {
        super(context);
        mContext = context;
        mType = type;
        init();
    }

    private void init() {
        LayoutInflater.from(mContext).inflate(R.layout.widget_single_module, this, true);
        mParentlayout = (LinearLayout) this.findViewById(R.id.parent_layout);
        mTitleTv = (TextView) this.findViewById(R.id.module_title);
        mImageOne = (ImageView) this.findViewById(R.id.module_image_one);
        mImageTwo = (ImageView) this.findViewById(R.id.module_image_two);
        mImageThree = (ImageView) this.findViewById(R.id.module_image_three);
        mImageFour = (ImageView) this.findViewById(R.id.module_image_four);

        if (mType.equals(TYPE_HOT)) {
            mTitleTv.setText("热门作品");
        } else if (mType.equals(TYPE_ASK)) {
            mTitleTv.setText("最新求P");
        } else {
            mTitleTv.setText("最新作品");
        }

        mParentlayout.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mContext, ModuleActivity.class);
                intent.putExtra(Constant.IntentKey.TITLE, mType);
                mContext.startActivity(intent);
            }
        });

        getData();
    }

    private void getData() {
        NetworkRequest.getInstance().getModuleList(1, 4, mType, mContext,
                new TupaiSubscriber<List<FeedItem>>(mContext) {
                    @Override
                    protected void requestSuccess(List<FeedItem> feedItems) {
                        if (feedItems == null || feedItems.size() == 0) {
                            return;
                        }
                        if (feedItems.size() >= 1 && feedItems.get(0).getMedias() != null && feedItems.get(0).getMedias().size() != 0) {
                            ImageLoadUtil.getInstance().loadImage(feedItems.get(0).getMedias().get(0).getPathname(), mImageOne, ImageLoadUtil.LoadOption.LOAD_SMALL);
                        }
                        if (feedItems.size() >= 2 && feedItems.get(1).getMedias() != null && feedItems.get(1).getMedias().size() != 0) {
                            ImageLoadUtil.getInstance().loadImage(feedItems.get(1).getMedias().get(0).getPathname(), mImageTwo, ImageLoadUtil.LoadOption.LOAD_SMALL);
                        }
                        if (feedItems.size() >= 3 && feedItems.get(2).getMedias() != null && feedItems.get(2).getMedias().size() != 0) {
                            ImageLoadUtil.getInstance().loadImage(feedItems.get(2).getMedias().get(0).getPathname(), mImageThree, ImageLoadUtil.LoadOption.LOAD_SMALL);
                        }
                        if (feedItems.size() >= 4 && feedItems.get(3).getMedias() != null && feedItems.get(3).getMedias().size() != 0) {
                            ImageLoadUtil.getInstance().loadImage(feedItems.get(3).getMedias().get(0).getPathname(), mImageFour, ImageLoadUtil.LoadOption.LOAD_SMALL);
                        }
                    }

                    @Override
                    protected void requestError(String error) {

                    }
                });
    }

    public void setTitle(String title) {
        mTitleTv.setText(title);
    }

    public void setImageList(List<String> list) {
        mImageList = list;
    }


}
